﻿/**
 * Database helper.
 * This class will copying database from resource to pad.
 */

package in.abip.chisame.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.content.Context;

/**
 * @author Julot.
 * @since Thurday, January 20th, 2011.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	public static String DB_PATH = "/data/data/in.abip.chisame/databases/";
	public static String DB_NAME = "chisame.db";
	private SQLiteDatabase _database;
	private final Context _context;
	
	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		this._context = context;
	}
	
	/**
	 * Copy database from resource if not exist.
	 * @throws IOException
	 */
	public void createDataBase() throws IOException{
		if (!checkDataBase()) {
			this.getReadableDatabase();
			try {
				copyDataBase();
			} catch (IOException e) {
				throw new Error("Error copying database");
			}
		}
	}
	
	/**
	 * Check database.
	 * @return boolean whether database readable.
	 */
	private boolean checkDataBase(){
		SQLiteDatabase checkDB = null;
		try {
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(
				myPath, 
				null, 
				SQLiteDatabase.OPEN_READONLY
			);
		}
		catch(SQLiteException e) {}
		if(checkDB != null)checkDB.close();
		return checkDB != null ? true : false;
	}
	
	/**
	 * Copy database from resource.
	 * @throws IOException
	 */
	private void copyDataBase() throws IOException{
		InputStream myInput = this._context.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}
	
	/**
	 * Download database from server.
	 * @param link url to database.
	 */
	public void downloadDataBase(String link) {
		try {
			URL url = new URL(link);
			HttpURLConnection c = (HttpURLConnection) url.openConnection();
			c.setRequestMethod("GET");
			c.setDoOutput(false);
			c.connect();
			InputStream is = c.getInputStream();
			
			File file = new File(DB_PATH);
			file.mkdirs();
			File outputFile = new File(file, DB_NAME);
			FileOutputStream fos = new FileOutputStream(outputFile);
			
			byte[] buffer = new byte[1024];
			int len1 = 0;
			while ((len1 = is.read(buffer)) != -1) {
				fos.write(buffer, 0, len1);
			}
			fos.close();
			is.close();
			c.disconnect();
		} catch (IOException e) {
			Log.d("log_tag", "Error: " + e);
		}
	}
	
	/**
	 * Open database for operation.
	 * @throws SQLException
	 */
	public void openDataBase() throws SQLException{
		String myPath = DB_PATH + DB_NAME;
		this._database = SQLiteDatabase.openDatabase(
			myPath, 
			null, 
			SQLiteDatabase.OPEN_READONLY
		);
	}
	
	@Override
	public synchronized void close() {
		if (this._database != null) this._database.close();
		super.close();
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
